package com.dz.risk.custom.riskFactorBacktracking;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.dz.risk.pojo.domain.RiskFactorBacktracking;
import com.dz.risk.service.RiskFactorBacktrackingService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 风险因子回溯导出
 *
 * @author Doke
 * @date 2021/7/1 16:10
 */
@Component
public class RiskFactorBacktrackingExport {

    private final RiskFactorBacktrackingService riskFactorBacktrackingService;

    @Autowired
    public RiskFactorBacktrackingExport(RiskFactorBacktrackingService riskFactorBacktrackingService) {
        this.riskFactorBacktrackingService = riskFactorBacktrackingService;
    }

    /**
     * 根据风险因子回溯版本导出
     *
     * @param outputStream 输出流
     * @param version      版本号
     */
    public void doExportByVersion(ServletOutputStream outputStream, String version) throws IOException {
        //根据版本查询出该版本下得所有风险因子回溯信息
        List<RiskFactorBacktracking> riskFactorBacktrackingList = riskFactorBacktrackingService.selectByParams(RiskFactorBacktracking.builder().version(version).build());

        //如果该版本得风险因子回溯有数据则填充进Excel
        if (!CollectionUtils.isEmpty(riskFactorBacktrackingList)) {
            Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams(), RiskFactorBacktracking.class, riskFactorBacktrackingList);
            sheets.write(outputStream);
            outputStream.close();
            sheets.close();
        }
    }
}
